package com.ruoyi.web.controller.module.property;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.domain.module.property.PropertyUser;
import com.ruoyi.system.domain.module.wx.WxUser;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.property.IPropertyUserService;
import com.ruoyi.system.service.module.wx.IWxUserService;
import com.ruoyi.system.service.system.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商家的管理用户wxuser的用户Controller
 *
 * @author ruoyi
 * @date 2022-08-20
 */
@Validated
@Api(value = "商家的管理用户wxuser的用户控制器", tags = {"商家的管理用户wxuser的用户管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/propertyUser")
public class PropertyUserController extends BaseController {

    private final IPropertyUserService propertyUserService;
    private final IEstateService estateService;
    private final IWxUserService wxUserService;
    private final ISysUserService sysUserService;

    /**
     * 查询商家的管理用户wxuser的用户列表
     */
    @ApiOperation("查询物业工作人员列表")
    @PreAuthorize("@ss.hasPermi('module:user:list')")
    @GetMapping("/list")
    public TableDataInfo<PropertyUser> list(@Validated PropertyUser propertyUser) {
        QueryWrapper<PropertyUser> qw = new QueryWrapper<>();
        qw.eq("sys_user_id",propertyUser.getSysUserId());
        TableDataInfo<PropertyUser> td = PageUtils.buildDataInfo(propertyUserService.page(PageUtils.buildPage(),qw));
        List<PropertyUser> list = td.getRows();
        for (PropertyUser estate:list){
            Estate estate1=estateService.findById(estate.getEstateId());
            estate.setEstateName(estate1.getEstateName());
            if(estate.getUserId()!=0){
                WxUser wxUser=wxUserService.findById(estate.getUserId());
                estate.setNickName(wxUser.getNickName());
            }else {
                estate.setNickName("未绑定");
            }

            SysUser sysUser=sysUserService.selectUserById(Long.valueOf(estate.getSysUserId()));
            estate.setUserName(sysUser.getNickName());
        }
        return td;
    }

    /**
     * 导出商家的管理用户wxuser的用户列表
     */
    @ApiOperation("导出查询物业工作人员列表")
    @PreAuthorize("@ss.hasPermi('module:user:export')")
    @Log(title = "商家的管理用户wxuser的用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<PropertyUser> export(@Validated PropertyUser propertyUser) {
        List<PropertyUser> list = propertyUserService.list();
        ExcelUtil<PropertyUser> util = new ExcelUtil<PropertyUser>(PropertyUser.class);
        return util.exportExcel(list, "商家的管理用户wxuser的用户");
    }

    /**
     * 获取商家的管理用户wxuser的用户详细信息
     */
    @ApiOperation("获取查询物业工作人员列表详细信息")
    @PreAuthorize("@ss.hasPermi('module:user:query')")
    @GetMapping("/{id}")
    public TableDataInfo<PropertyUser> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        QueryWrapper<PropertyUser> qw = new QueryWrapper<>();
        qw.eq("id",id);
        TableDataInfo<PropertyUser> td = PageUtils.buildDataInfo(propertyUserService.page(PageUtils.buildPage(),qw));
        List<PropertyUser> list = td.getRows();
        for (PropertyUser estate:list){
            Estate estate1=estateService.findById(estate.getEstateId());
            estate.setEstateName(estate1.getEstateName());
            WxUser wxUser=wxUserService.findById(estate.getUserId());
            estate.setNickName(wxUser.getNickName());
        }
        return td;
    }

    /**
     * 新增商家的管理用户wxuser的用户
     */
    @ApiOperation("新增查询物业工作人员列表用户")
    @PreAuthorize("@ss.hasPermi('module:user:add')")
    @Log(title = "商家的管理用户wxuser的用户", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody PropertyUser propertyUser) {
        /*QueryWrapper<PropertyUser> qw=new QueryWrapper<>();
        qw.eq("estate_id",propertyUser.getEstateId());
        List<PropertyUser> list=propertyUserService.list(qw);
        Integer name = null;
        for (PropertyUser user:list){
            name=user.getUserId();
        }
        if(name==null){
            propertyUser.setUserId(propertyUser.getEstateId());
            return toAjax(propertyUserService.save(propertyUser) ? 1 : 0);
        }
        return toAjax(0);*/
        SysUser sysUser= sysUserService.selectUserById(Long.valueOf(propertyUser.getSysUserId()));
        propertyUser.setUserName(sysUser.getNickName());
        propertyUser.setUserType(Long.valueOf(sysUser.getUserType()));
        propertyUser.setPhoneNum(sysUser.getPhoneNum());
        return toAjax(propertyUserService.save(propertyUser) ? 1 : 0);
    }

    /**
     * 修改商家的管理用户wxuser的用户
     */
    @ApiOperation("修改查询物业工作人员列表的用户")
    @PreAuthorize("@ss.hasPermi('module:user:edit')")
    @Log(title = "商家的管理用户wxuser的用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody PropertyUser propertyUser) {
        /*QueryWrapper<PropertyUser> qw=new QueryWrapper<>();
        qw.eq("estate_id",propertyUser.getEstateId());
        List<PropertyUser> list=propertyUserService.list(qw);
        Integer name = null;
        for (PropertyUser user:list){
            name=user.getUserId();
        }
        if(name==null||name==propertyUser.getUserId()){
            propertyUser.setUserId(propertyUser.getEstateId());
            return toAjax(propertyUserService.updateById(propertyUser) ? 1 : 0);
        }
        return toAjax(0);*/
        SysUser sysUser= sysUserService.selectUserById(Long.valueOf(propertyUser.getSysUserId()));
        propertyUser.setUserName(sysUser.getNickName());
        propertyUser.setUserType(Long.valueOf(sysUser.getUserType()));
        propertyUser.setPhoneNum(sysUser.getPhoneNum());
        return toAjax(propertyUserService.updateById(propertyUser) ? 1 : 0);
    }

    /**
     * 删除商家的管理用户wxuser的用户
     */
    @ApiOperation("删除查询物业工作人员列表的用户")
    @PreAuthorize("@ss.hasPermi('module:user:remove')")
    @Log(title = "商家的管理用户wxuser的用户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(propertyUserService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
